﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.Commons;
using YT.Controls;
using YT.DB;
using YT.AdvanceCommons; 
  
namespace YT.XH.MF
{
    public partial class frmSickPersonMtn : Form
    {
        private string mstrTableName = "mSickPerson";
        private bool mblnIsInSelecting = false;
        private bool mblnIsInUpdate = false;
        private DBAccess mdbcon = new DBAccess(false, false);
        private ArrayList mArrayControl = new ArrayList();
        private string mstrRecID = string.Empty;
        private FormStatus mFormStatus= new FormStatus() ;
        private Form frmMContraindication = null;
        private TitleType FromType;
        private bool mblnFromOtherMtn = false;
        public frmSickPersonMtn()
        {
            InitializeComponent();
        }


        public frmSickPersonMtn(string strSickRecID)
        {
            InitializeComponent();
            string strRecID= DataFunctions.GetFieldValue ("mSickPerson","RecID"," SickRecID='"+ strSickRecID +"'");
            mstrRecID = strRecID;            
            mblnFromOtherMtn = true;            
        }


        private void Contraindication()
        {
            
            Type tpType;
            string strSickRecId = txtSickRecID.Text;

            
            object[] args = new object[1];
            args[0] = strSickRecId;
            
            if ( FromType == TitleType.SuddenDeafness  )
            {
                tpType = Type.GetType("YT.XH.SuddenDeafness.frmContraindicationMtn,YT.XH.SuddenDeafness");
            }
            else if (FromType == TitleType.MeniereDisease)
            {
                tpType = Type.GetType("YT.XH.MeniereDisease.frmMDContraindicationMtn,YT.XH.MeniereDisease");
            }
            else 
            {
                return;
            }
            
            if (tpType == null)
            {
                MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            
            { 
                object obj = null;
                Type[] ptype = new Type[1];
                ptype.SetValue(typeof (String), 0);
                object oRpt = frmMContraindication;
                System.Reflection.MethodInfo mInfo = oRpt.GetType().GetMethod("FillContraindication", ptype);
                object[] Param = { strSickRecId };
                if (mInfo != null)
                {
                    obj = mInfo.Invoke(oRpt, Param);
                }              
           }
        }

        private bool UpdateContraindication()
        {
            string strSickRecId = txtSickRecID.Text;  
            object obj = null;           
            object oRpt = frmMContraindication;
            if (oRpt != null)
            {
                System.Reflection.MethodInfo mInfo = oRpt.GetType().GetMethod("UpdateData");
                object[] Param = { strSickRecId };
                if (mInfo != null)
                {
                    obj = mInfo.Invoke(oRpt, null );
                }
            }

            string strSQL="select count(*) from mContraindicationDetail where SickRecID='"+ txtSickRecID.Text    +"'";
            DataRow drCheck = mdbcon.GetDataFirstRow(strSQL);
            if (drCheck != null)
            {
                int intCount = CvtFunc.ToInt(drCheck[0]);
                if (intCount > 0)
                    cmbHasContraindication.Text = "有";
                //else
                //    cmbHasContraindication.Text = "无";
            }
            return true;
        }

        private void frmSickPersonMtn_Load(object sender, EventArgs e)
        {
            
            if (SysVar.CurrentMenuInfo.strMenuName.IndexOf("患者诊疗", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                toolStrip2.Visible = true;
                toolStrip2.Enabled = false;
            }
            else
            {
                toolStrip2.Visible = false;
                tbcMain.Top = tbcMain.Top -22;
            }

            if ( mblnFromOtherMtn ==false ) mstrRecID = "";
            if (SysVar.CurrentMenuInfo.strMenuId.Substring(0, 3) == "100")
                FromType = TitleType.MeniereDisease;
            else if (SysVar.CurrentMenuInfo.strMenuId.Substring(0, 3) == "200")
                FromType = TitleType.SuddenDeafness;
            else if (SysVar.CurrentMenuInfo.strMenuId.Substring(0, 3) == "300")
                FromType = TitleType.Dizziness;
            else if (SysVar.CurrentMenuInfo.strMenuId.Substring(0, 3) == "400")
            {
                FromType = TitleType.NOSE;
                toolStripButton1.Visible = false;
                知情同意书ToolStripMenuItem.Visible = false;
            }
            else if (SysVar.CurrentMenuInfo.strMenuId.Substring(0, 3) == "600")
            {
                FromType = TitleType.IgE;
            }
            else
                FromType = TitleType.OTHERS;

            if ( FromType == TitleType.Dizziness || FromType == TitleType.NOSE || FromType == TitleType.OTHERS || FromType == TitleType.IgE )
                this.tbcMain.TabPages.Remove(tabContraindication);
            else
            {
                if (frmMContraindication == null)
                {
                    Type tpType=null ;
                    string strSickRecId = txtSickRecID.Text;
                    if ( FromType == TitleType.SuddenDeafness) 
                        tpType = Type.GetType("YT.XH.SuddenDeafness.frmContraindicationMtn,YT.XH.SuddenDeafness");
                    else if ( FromType == TitleType.MeniereDisease )
                        tpType = Type.GetType("YT.XH.MeniereDisease.frmMDContraindicationMtn,YT.XH.MeniereDisease");

                    object[] args = new object[1];
                    args[0] = strSickRecId;
                    object mRouteObj = Activator.CreateInstance(tpType, args);
                    frmMContraindication = (Form)mRouteObj;
                    frmMContraindication.TopMost = false;
                    frmMContraindication.TopLevel = false;
                    frmMContraindication.Dock = DockStyle.Fill;
                    frmMContraindication.FormBorderStyle = FormBorderStyle.None;
                    tabContraindication.Controls.Add(frmMContraindication);
                    frmMContraindication.Location = new Point(0, 0);
                    frmMContraindication.Dock = DockStyle.Fill;
                    frmMContraindication.Size = new Size(this.tabContraindication.Width, this.tabContraindication.Height);
                    frmMContraindication.Visible = true;
                }
            }
            //mFormStatus = FormStatus.Loading;
            mArrayControl.Clear();
            foreach (Control con in this.pnlDetail.Controls)
            {
                mArrayControl.Add(con);
                con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText); 
            }

            foreach (Control con in this.panel1.Controls)
            {
                mArrayControl.Add(con);
                con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText); 
            }

            foreach (Control con in this.panel2.Controls)
            {
                mArrayControl.Add(con);
                con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText); 
            }

            foreach (Control con in this.panel3.Controls)
            {
                mArrayControl.Add(con);
                con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText); 
            }
            mFormStatus = FormStatus.Query;
            InitControl();
            this.Text = SysVar.CurrentMenuInfo.strMenuName;
            dgvMain.ConnectionString = DBAccess.ConnectionDBString;
            string strSQL=DataFunctions.GetLookUpSQL(this.mstrTableName , "");
            string strType = string.Empty;

            if (FromType == TitleType.MeniereDisease)
                strType = TitleType.MeniereDisease.ToString();
            else if (FromType == TitleType.SuddenDeafness)
                strType = TitleType.SuddenDeafness.ToString();
            else if (FromType == TitleType.Dizziness)
                strType = TitleType.Dizziness.ToString();
            else if (FromType == TitleType.NOSE)
                strType = TitleType.NOSE.ToString();
            else if (FromType == TitleType.IgE)
                strType = TitleType.IgE.ToString();
            else
                strType = TitleType.OTHERS.ToString(); 

            string strWhereSQL = BaseFunctions.GetSqlParts(strSQL, SqlOption.SqlPartWhere);
            if ( strWhereSQL !="")
                strWhereSQL += " and Type='" + strType + "'";
            else
                strWhereSQL += " where  Type='" + strType + "'";
            strSQL = BaseFunctions.GetSqlParts(strSQL, SqlOption.SqlPartMain) + strWhereSQL + BaseFunctions.GetSqlParts(strSQL, SqlOption.SqlPartOrderBy); ;
            dgvMain.RowsPerPage = 200000;
            dgvMain.TableName = this.mstrTableName;
            this.btnExit.Visible = false;
            dgvMain.Sql = strSQL;            
            SetButtonStatus();
            this.btnExit.Visible = true;
            this.dgvMain.Columns["序号"].SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dgvMain.Columns["序号"].Width = 50;
            if (this.dgvMain.CurrentRow != null && mblnFromOtherMtn ==false )
            {
                this.mstrRecID = CvtFunc.ToString(this.dgvMain.CurrentRow.Cells["RecID"].Value);                
                FillTextBox();
                FillTextBox();
            }
            timer1.Start();
            if (mblnFromOtherMtn == true)
            {
                this.dgvMain.FindRecord(mstrRecID, "RecID");
                FillTextBox();
                tbcMain.TabPages.Remove(tabeOverview);
                tbcMain.SelectedTab = tabDetail;
            }
        }

        private void frmSickPersonMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (mblnFromOtherMtn == false) SysVar.marrFormList.Remove(this.Name);            
            if ( mblnFromOtherMtn ==false ) SysVar.SickRecID = "";
            mdbcon.CloseCon();
        }

        private void SelectNextText(object sender, KeyEventArgs  e)
        {          
            if (e.KeyCode== Keys.Return )
            SelectNextControl((Control)sender, true, true,true, false);  
        }
        private bool VaildCheck()
        {
            Control con = null;
            string strMsgBox= string.Empty ;
            if (txtSickID.Text.Trim() == "" && FromType!= TitleType.NOSE )
            {
                strMsgBox = "患者编号不能为空！";
                con = txtSickID;
            }
            else if (txtSickName.Text.Trim() == "")
            {
                strMsgBox = "患者姓名不能为空！";
                con = txtSickName;
            }
            else if (txtID.Text.Trim() == "" && ( FromType != TitleType.NOSE && FromType != TitleType.IgE))
            {
                strMsgBox = "身份证号不能为空.";
                con = txtID;
            }
            else if (CvtFunc.ToInt(txtAge.Text) <= 1)
            {
                strMsgBox = "请填写出生年月";
                con = dtpBirth;
            }
            else if (cmbHasContraindication.Text == "" && (FromType != TitleType.NOSE && FromType != TitleType.OTHERS))
            {
                strMsgBox = "请选择禁忌症。";
                con = cmbHasContraindication;
            }
            else if (txtHeight.Text == "")
            {
                strMsgBox = "请填写身高。";
                con = txtHeight;
            }
            else if (txtWeight.Text == "")
            {
                strMsgBox = "请填写体重。";
                con = txtWeight;
            } 
            
            if (txtID.Text.Trim().Length == 15 || txtID.Text.Trim().Length == 18)
            { 
            
            }
            else if ( FromType!= TitleType.NOSE && FromType != TitleType.IgE )
            {
                strMsgBox = "错误的身份证号，请检查后重新输入.";
                con = txtID;
            }

            if ( strMsgBox!="")
            {
                MessageBox.Show(strMsgBox, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (con != null) con.Focus();
                return false ;
            }
            
            return true ;
        }

        private bool UpdateData()
        {
            mblnIsInUpdate = true;
            if (mdbcon.Con  == null) return false;
            mdbcon.BeginTran();
            UpdateContraindication();
            if (!VaildCheck())
            {
                mdbcon.RollbackTran();
                return false;
            }
            
            bool blnIsNew = false;
            string strFileds = string.Empty;
            string strSQL = string.Empty;
            strSQL = "select * from " + mstrTableName + " where recid='" + mstrRecID + "'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow == null)
            {
                drRow = mdbcon.GetDataTable(strSQL).NewRow();
                blnIsNew = true;
            }
            strFileds = DataFunctions.GetUpdateSQL(mstrTableName, "");
            Panel pnlContronl =null ;
            for (int i = 0; i <= 3; i++)
            {
                if (i == 0) pnlContronl = this.pnlDetail;
                if (i == 1) pnlContronl = this.panel1;
                if (i == 2) pnlContronl = this.panel2;
                if (i == 3) pnlContronl = this.panel3;

                foreach (Control conTxtBox in pnlContronl.Controls)
                {
                    drRow["RecID"] = CvtFunc.ToInt(mstrRecID);
                    foreach (string strField in strFileds.Split(','))
                    {
                        string strValue = strField.Trim();
                        if (GetFieldName(conTxtBox.Name).ToUpper().Trim() == strValue.ToUpper().Trim())
                        {
                            if (conTxtBox.GetType().Name == "TextBox" || conTxtBox.GetType().Name == "ComboBox")
                            {
                                if (drRow[strValue].GetType().Name == "Int32")
                                    drRow[strValue] = CvtFunc.ToInt(conTxtBox.Text);
                                else if (drRow[strValue].GetType().Name == "Decimal")
                                    drRow[strValue] = CvtFunc.ToDouble(conTxtBox.Text);
                                else
                                    drRow[strValue] = CvtFunc.ToString(conTxtBox.Text);
                            }
                            else if (conTxtBox.GetType().Name == "DateTimePicker")
                                drRow[strValue] = CvtFunc.ToDateTime(((DateTimePicker)conTxtBox).Value);
                            else if (conTxtBox.GetType().Name == "TextRadioBox")
                                drRow[strValue] = CvtFunc.ToBoolean(((TextRadioBox)conTxtBox).Text);
                            else if (conTxtBox.GetType().Name == "CheckBox")
                                drRow[strValue] = CvtFunc.ToBoolean(((CheckBox)conTxtBox).Checked);
                        }
                    }
                }               
            }            
            if (blnIsNew == true)
                DataFunctions.UpdateTable(mdbcon, strFileds, mstrTableName, drRow, FormStatus.AddNew);
            else                
                DataFunctions.UpdateTable(mdbcon, strFileds, mstrTableName, drRow, FormStatus.Update);

            mstrRecID = CvtFunc.ToString(drRow["RecID"]);
            
            strSQL ="update mSickPerson set Type='"+ FromType.ToString () +"' where RecId='"+ mstrRecID  +"' ";
            mdbcon.ExecuteSql(strSQL);

            if (!CheckContraindication(mdbcon) && FromType == TitleType.SuddenDeafness)
            {
                this.tbcMain.SelectTab(tabContraindication);
                mdbcon.RollbackTran();
                return false;
            }
            mdbcon.CommitTran();
            mblnIsInUpdate = false;
            if (mFormStatus == FormStatus.AddNew)
            {
                SysVar.SickRecID = txtSickRecID.Text; 
                mFormStatus = FormStatus.Update;
            }
            
                 
            return true;
        }
        
        private bool Delete()
        {
            try
            {
                if (mstrRecID == "") return true; 
                if (BaseFunctions.SureDelete())
                {

                    mdbcon.BeginTran();
                    string strSQL = "";
                    string strTableName="";
                    string strSickRecID = txtSickRecID.Text;

                    strTableName="EffectAfterTreateHdr";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);  
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="EffectAfterTreateResult";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="ExamBeforeTreatHdr";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="ExamBeforTreatResult";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="JocobsonHdr";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="JocobsonResult";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="mContraindication";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="mContraindicationDetail";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseCAPExamDetail";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseCAPExamHdr";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseDrTreatHistoryDtl";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseLabExamDtl";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseLabExamHdr";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseNormalVisitRecordDtl";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseSkinPrickTestDetail";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseSkinPrickTestHdr";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseTreatmentHistoryDtl";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseVisitRecordDtl";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="NoseVisitWeek";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="THIResult";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName="titleResult";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strTableName = "UntowardEffectResult";
                    strSQL = "Delete from {0} where SickRecID='{1}'";
                    strSQL = string.Format(strSQL, strTableName, strSickRecID);
                    mdbcon.ExecuteSql(strSQL);

                    strSQL = "Delete from " + this.mstrTableName + " where RecID=" + this.mstrRecID + "";
                    mdbcon.ExecuteSql(strSQL);

                    strSQL = "Delete smsoutbox where sickRecID='" + strSickRecID + "'";
                    mdbcon.ExecuteSql(strSQL);


                    mdbcon.CommitTran(); 
                }
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
        }
        
        private void FillTextBox()
        {
            string strSQL = string.Empty;
            string[] strFieldName;
            strFieldName = DataFunctions.GetUpdateSQL(mstrTableName, "").Split(new char[] { ',' });

            strSQL = "select top 1 * from " + mstrTableName + " where recid="+ this.mstrRecID +"";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                foreach (string strField in strFieldName)
                {
                    string strValue = strField.Trim();
                    foreach (Control Con in mArrayControl)
                    {
                        if (GetFieldName(Con.Name).ToUpper() == strValue.ToUpper())
                        {
                            if (Con.GetType().Name == "TextBox")
                                Con.Text = CvtFunc.ToString(drRow[strValue]);
                            else if (Con.GetType().Name == "DateTimePicker")
                            {
                                ((DateTimePicker)Con).Value = CvtFunc.ToDateTime(drRow[strValue]);                             
                            }
                            else if (Con.GetType().Name == "TextRadioBox")
                                Con.Text = (CvtFunc.ToBoolean(drRow[strValue]) ? "Yes" : "No");
                            else if (Con.GetType().Name == "CheckBox")
                                ((CheckBox)Con).Checked = CvtFunc.ToBoolean(drRow[strValue]);
                            else if (Con.Name == "chkHasXC")
                                chkHasXC.Checked = (CvtFunc.ToBoolean(drRow[strValue]) ? true : false);
                            else if (Con.Name == "chkTrack")
                                chkTrack.Checked = (CvtFunc.ToBoolean(drRow[strValue]) ? true : false);
                            else if (Con.Name == "chkNeedMsg")
                                chkNeedMsg.Checked = ( CvtFunc.ToBoolean (drRow[strValue]) ? true : false ); 
                            else if (Con.Name == this.cmbSex.Name)
                            {
                                if (CvtFunc.ToString(drRow[strValue]) == "男")
                                    ((ComboBox)Con).SelectedIndex = 0;
                                else
                                    ((ComboBox)Con).SelectedIndex = 1;
                            }
                            else if (Con.Name == this.cmbMarried.Name)
                            {
                                if (CvtFunc.ToString(drRow[strValue]) == "已婚")
                                    ((ComboBox)Con).SelectedIndex = 0;
                                else
                                    ((ComboBox)Con).SelectedIndex = 1;
                            }
                            else if (Con.Name == this.cmbHasContraindication.Name)
                            {
                                if (CvtFunc.ToString(drRow[strValue]) == "有")
                                    ((ComboBox)Con).SelectedIndex = 1;
                                else if (CvtFunc.ToString(drRow[strValue]) == "无")
                                    ((ComboBox)Con).SelectedIndex = 2;
                                else
                                    ((ComboBox)Con).SelectedIndex = 0;
                            }
                            else if (Con.Name == this.cmbEducational.Name)
                            {
                                Con.Text = CvtFunc.ToString(drRow[strValue]);
                            }
                        }
                    }
                }
                mstrRecID = CvtFunc.ToString(drRow["RecID"]);
                txtCurOperation.Text = CvtFunc.ToString(drRow["CurOperation"]);
                Contraindication();
                if (txtSickRecID.Text.Trim() != "")
                {
                    if (mblnFromOtherMtn == false)
                        SysVar.SickRecID = txtSickRecID.Text;
                }
                txtHospitalName.Text =DataFunctions.GetFieldValue ("mHospital","HospitalName"," hospitalID='"+ txtHospitalID.Text   +"'");
                if (txtCurOperation.Text.Trim() != "")
                {
                    cmbHasContraindication.Enabled = false;
                    chkUseKFJS.Enabled = false;
                    chkUseRS.Enabled = false;
                    chkCuredInOtherHosiptal.Enabled = false;
                    dtpFirstDate.Enabled = false;
                    dtpPainDate.Enabled = false;
                    btnSel.Enabled = false;
                    SetContraindicationReadOnly(false );
                }
                else
                {
                    cmbHasContraindication.Enabled = true ;
                    chkUseKFJS.Enabled = true;
                    chkUseRS.Enabled = true;
                    chkCuredInOtherHosiptal.Enabled = true;
                    dtpFirstDate.Enabled = true;
                    dtpPainDate.Enabled = true;
                    btnSel.Enabled = true;
                    SetContraindicationReadOnly(true );
                }
                txtAge.Text = BaseFunctions.FormatNumeric(txtAge.Text, "N.0");
                txtWeight.Text = BaseFunctions.FormatNumeric(txtWeight.Text, "N.0");                 
            }
        }


        private void SetContraindicationReadOnly( bool blnTrue )
        {
            if (frmMContraindication==null ) return;
            object obj = null;
            Type[] ptype = new Type[1];
            ptype.SetValue(typeof(bool ), 0);
            object oRpt = frmMContraindication;
            System.Reflection.MethodInfo mInfo = oRpt.GetType().GetMethod("SetReadonly", ptype);
            object[] Param = { blnTrue  };
            if (mInfo != null)
            {
                obj = mInfo.Invoke(oRpt, Param);
            }    
        }

        private string GetFieldName(string con)
        {
            if (con.Length > 3)
            {
                return con.Substring(3, con.Length - 3);
            }
            return "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {           
            if (UpdateData())
            {
                if (mblnFromOtherMtn == false)
                {
                    this.dgvMain.FindRecord(mstrRecID, "RecID");
                    this.tbcMain.SelectTab(tabeOverview);
                }
                else
                {
                    this.Close();
                }
               // this.dgvMain.RefreshData();
                
            }
            string strSickRecID = txtSickRecID.Text;
            if (!CvtFunc.ToBoolean(DataFunctions.CheckRecordExist("msickperson", strSickRecID, "SickRecID")))
            {
                string strSQL = "Delete from mContraindication where sickRecID='" + strSickRecID + "'";
                mdbcon.ExecuteSql(strSQL);
                strSQL = "delete from mContraindicationDetail where SickRecID='" + strSickRecID + "'";
                mdbcon.ExecuteSql(strSQL);
            }
        }

        private bool CheckContraindication(DBAccess dbcon)
        {
            if (FromType != TitleType.SuddenDeafness) return true;
            if (cmbHasContraindication.Text == "") return true;
            if (cmbHasContraindication.Text == "无") return true;
            string strSQL = string.Empty;
           
            strSQL = "select * from mContraindication where SickRecID='" + txtSickRecID.Text + "' and Type ='" + FromType + "'";
            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
            if (drCheck == null)
            {
                MessageBox.Show("您必须选择此患者的禁忌症。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;            
        }

        private void SetButtonStatus()
        {
            if (mFormStatus == FormStatus.AddNew)
            {
                btnNew.Enabled = false;
                btnDel.Enabled = false;
                btnSave.Enabled = true;                
            }
            else if (mFormStatus == FormStatus.Update)
            {
                btnSave.Enabled = true;
                btnNew.Enabled = true;
                btnDel.Enabled = true;
            }
            else if (mFormStatus == FormStatus.Query)
            {
                btnSave.Enabled = false;
                btnNew.Enabled = true ;
                btnDel.Enabled = false;
            }
            SetControl();
        }
        private void SetControl()
        {
            if (mFormStatus == FormStatus.AddNew)
            {                
                txtSickID.Enabled = true;
                txtSickID.Focus();
            }
            else
            {
                txtSickID.Enabled = false;
                txtSickName.Focus();  
            }
         
        }

        private void InitControl()
        {
            
            DBAccess dbcon = new DBAccess(false, false);
            cmbMarried.Items.Clear();
            cmbMarried.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbMarried.Items.Add("已婚");
            cmbMarried.Items.Add("未婚");
            cmbMarried.SelectedIndex = 0;

            cmbSex.Items.Clear();
            cmbSex.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbSex.Items.Add("男");
            cmbSex.Items.Add("女");
            cmbSex.SelectedIndex = 0;

            cmbHasContraindication.Items.Clear();
            cmbHasContraindication.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbHasContraindication.Items.Add("");
            cmbHasContraindication.Items.Add("有");
            cmbHasContraindication.Items.Add("无");  

            
            txtSickID.Enabled = true;
            Panel pnlCon = null;

            for (int intRow = 0; intRow <= 3; intRow++)
            {
                if (intRow == 0) pnlCon = pnlDetail;
                if (intRow == 1) pnlCon = panel1;
                if (intRow == 2) pnlCon = panel2;
                if (intRow == 3) pnlCon = panel3; 
                foreach (Control con in pnlCon.Controls)
                {
                    if (con.GetType().Name == "TextBox")
                        con.Text = "";
                    else if (con.GetType().Name == "DateTimePicker")
                        ((DateTimePicker)con).Text = DateTime.Now.Date.ToString()  ;
                    else if (con.GetType().Name == "CheckBox")
                        ((CheckBox)con).Checked = false;
                }
            }
            if (FromType == TitleType.SuddenDeafness)
            {
                btnTreatmentEffectTitle_SD.Visible = true;
                btnTreatmentEffectTitle.Visible = false;
            }
            else
            {
                btnTreatmentEffectTitle_SD.Visible = false ;
                btnTreatmentEffectTitle.Visible = true ;
                btnAfterTreat.Visible = false;
                btnUntowardEffect.Visible = false;                  
            }

            chkCuredInOtherHosiptal.Checked = false;
            chkUseKFJS.Checked = false;
            chkUseRS.Checked = false;
            
            if (FromType == TitleType.Dizziness)
            {
                //btn治疗方案选择.Visible = false;
            }

            if (FromType == TitleType.SuddenDeafness || FromType == TitleType.MeniereDisease )
            {
                btnSel.Visible = true;
                if (FromType == TitleType.SuddenDeafness)
                    chkCuredInOtherHosiptal.Visible = true;
                else
                    chkCuredInOtherHosiptal.Visible = false;
                //chkUseKFJS.Visible = true;
                //chkUseRS.Visible = true;
            }
            else
            {
                btnSel.Visible = false ;
                chkCuredInOtherHosiptal.Visible = false;
                chkUseKFJS.Visible = false;
                chkUseRS.Visible = false;
            }

            #region SetControl MaxLength
            string strSQL = @"    select c.name as Type,a.name,a.length from [dbo].syscolumns  a  
                            inner join  [dbo].sysobjects b on a.id=b.id 
                            inner join SYSTYPES C  on a.xtype=c.xtype
                            inner join syscomments D on a.cdefault=d.id
                            and a.id = object_id( '" + this.mstrTableName + "') and (c.name='varchar' or c.name='char')";
            
            DataTable dtTable = dbcon.GetDataTable(strSQL);

            foreach (DataRow drRow in dtTable.Rows)
            {
                foreach (Control con in mArrayControl)
                {
                    if (con.GetType().Name == "TextBox")
                    {
                        if (CvtFunc.ToString(drRow["Name"]).Trim().ToUpper() == GetFieldName(con.Name).ToUpper().Trim())
                            ((TextBox)con).MaxLength = CvtFunc.ToInt(drRow["Length"]);
                    }
                }
            }

            if (mFormStatus == FormStatus.AddNew)
            {
                strSQL = @"    select c.name as Type,a.name,a.length,d.text as DftText from [dbo].syscolumns  a  
                            inner join  [dbo].sysobjects b on a.id=b.id 
                            inner join SYSTYPES C  on a.xtype=c.xtype
                            inner join syscomments D on a.cdefault=d.id
                            and a.id = object_id( '" + this.mstrTableName + "') ";
                DataTable dtDft = dbcon.GetDataTable(strSQL);
  
                foreach (DataRow drRow in dtDft.Rows)
                {
                    foreach (Control con in mArrayControl)
                    {
                        if (con.GetType().Name == "TextBox")
                        {
                            if (CvtFunc.ToString(drRow["Name"]).Trim().ToUpper() == GetFieldName(con.Name).ToUpper().Trim())
                            {
                                string strDftValue = CvtFunc.ToString(drRow["DftText"]);

                                if (strDftValue == "('')") strDftValue = string.Empty;
                                 strDftValue = strDftValue.Replace("(","");
                                 strDftValue= strDftValue.Replace (")","");
                                ((TextBox)con).Text = CvtFunc.ToString(strDftValue);
                            }
                        }
                    }
                }
                
                cmbEducational.Text = "";
            }
            dbcon.CloseCon();
            #endregion

            #region Set Function Hellp
            if (FromType == TitleType.MeniereDisease || FromType == TitleType.Dizziness  )
            {
                btnTreatmentEffectTitle.ToolTipText = "患者停止治疗后的1月、3月、6月、12月、24月进行评价。";
            }
            else if (FromType == TitleType.SuddenDeafness)
            {
                btnTreatmentEffectTitle_SD.ToolTipText = "患者停止治疗后的第7天,第14天,第28天进行评价"; 
            }            


           
            #endregion
            btnDel.Visible = false;
            if (FromType == TitleType.NOSE)
            {
                btnDel.Visible = true;
                btnAllMsg.Visible = true;
                btnSendMsg.Visible = true;
            }
            else
            {
                btnDel.Visible = false ;
                btnAllMsg.Visible = false ;
                btnSendMsg.Visible = false ;
            }
            if (FromType == TitleType.NOSE)
            {
                panel1.Visible = false;
                panel2.Visible = true;
                panel3.Visible = true;
                panel3.Top = panel1.Top;
                txtRemark.Left = txtPhone.Left;
                label21.Left　 = label16.Left ;
                txtRemark.Top = txtRemark.Top + 30;
                label21.Top = txtRemark.Top;                
                chkHasXC.Visible = true;
                txtID.Visible = false;
                lblDescID.Visible = false;
                label18.Visible = false; 
                txtCaseID.Visible = false;
            }
            else
            {
                panel1.Visible = true ;
                panel2.Visible = false ;
                panel3.Visible = true;
                panel1.Top = panel2.Top;
               // panel3.Top = panel3.Top - panel2.Height;
                chkHasXC.Visible = false;
                txtID.Visible = true;
                lblDescID.Visible = true;
                label18.Visible = true ;
                txtCaseID.Visible = true ;
            }

            if (FromType == TitleType.IgE)
            {
                知情同意书ToolStripMenuItem.Visible = false;
                toolStripButton1.Visible = false;
            }
            if (mFormStatus == FormStatus.AddNew)
            {
                txtAge.ReadOnly = false;
            }
            else
            {
                txtAge.ReadOnly = true;
            }
        }

        private void SetSickID()
        {

            string strMaxValue = DataFunctions.GetFieldValue(this.mstrTableName, "max(SickID) as SickID ", " 1=1 ");
            string strMaxValueDate = string.Empty;
            int intMaxVauleSeqNo = 0;
            if (strMaxValue == "")
            {
                strMaxValueDate = DateTime.Now.Date.Year.ToString() + DateTime.Now.Date.Month.ToString().PadLeft(2, '0') + DateTime.Now.Date.Day.ToString().PadLeft(2, '0') + "001";
                txtSickID.Text = strMaxValueDate;
            }
            else
            {
                strMaxValueDate = strMaxValue.Substring(0, 8);
                string strCurDate = DateTime.Now.Date.Year.ToString() + DateTime.Now.Date.Month.ToString().PadLeft(2, '0') + DateTime.Now.Date.Day.ToString().PadLeft(2, '0');
                if (strMaxValueDate == strCurDate)
                {
                    intMaxVauleSeqNo = CvtFunc.ToInt(strMaxValue.Substring(8, 3));
                    intMaxVauleSeqNo++;
                    txtSickID.Text = strMaxValueDate + CvtFunc.ToString(intMaxVauleSeqNo).PadLeft(3, '0');
                }
                else
                {
                    strMaxValueDate = DateTime.Now.Date.Year.ToString() + DateTime.Now.Date.Month.ToString().PadLeft(2, '0') + DateTime.Now.Date.Day.ToString().PadLeft(2, '0') + "001";
                    txtSickID.Text = strMaxValueDate;
                }       
                
            }

            string strPrefix = DataFunctions.GetFieldValue("mHospital", "Prefix", " 1=1 and isCurHospital=1 ");
            string strPrefixType = SysVar.CurrentMenuInfo.strMenuId.Substring(0, 1);  
            txtSickRecID.Text =strPrefix + strPrefixType + txtSickID.Text ;
            txtHospitalID.Text = SysVar.HospitalID;
            txtHospitalName.Text = SysVar.HospitalName;  
        }



        private void dgvMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgvMain.CurrentRow!= null && BaseFunctions.IsDatagridCell(dgvMain, e))
            {
                mstrRecID = CvtFunc.ToString ( this.dgvMain.CurrentRow.Cells["RecID"] );
                this.tbcMain.SelectTab(tabDetail);  
            }
        }

        private void dgvMain_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvMain.Rows[e.RowIndex] != null)
            {
                mstrRecID = CvtFunc.ToString ( this.dgvMain.Rows[e.RowIndex].Cells["RecID"].Value ); 
                FillTextBox();
                if (mblnFromOtherMtn == false)
                {
                    SysVar.SickRecID = txtSickRecID.Text;
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (Delete())
            {
                this.mFormStatus = FormStatus.Query; 
                this.tbcMain.SelectTab(tabeOverview);
                SysVar.SickRecID = "";
            }
        }

        private void tbcMain_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (this.dgvMain.CurrentRow == null && mFormStatus == FormStatus.Query ) { e.Cancel = true; return; }
            mblnIsInSelecting = true;
            if (e.TabPage == tabDetail)
            {
                string strSQL = "select * from mContraindication where sickRecID='" + txtSickRecID.Text   + "'";
                DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
                if (drRow != null)
                {
                    cmbHasContraindication.Text = "有";
                }
                else 
                {
                    strSQL = "select * from mContraindicationDetail where SickRecID='" + txtSickRecID.Text + "'";
                    drRow = mdbcon.GetDataFirstRow(strSQL);
                    if (drRow != null)
                    {
                        cmbHasContraindication.Text = "有";
                    }
                    else
                    {
                        cmbHasContraindication.Text = "无";
                    }
                }
            }
            if (cmbHasContraindication.Text == "有" && FromType != TitleType.SuddenDeafness ) UpdateContraindication();
            mblnIsInSelecting = false;
            if (e.TabPage == tabDetail  )
            {                
                if (mFormStatus == FormStatus.AddNew) 
                    mstrRecID = "";
                else
                    mstrRecID = CvtFunc.ToString(this.dgvMain.CurrentRow.Cells["RecID"].Value);

                if (mFormStatus == FormStatus.Query) mFormStatus = FormStatus.Update;
                SetButtonStatus();
                toolStrip2.Enabled = true;
            }
            else if (e.TabPage == tabContraindication)
            {
                if (mFormStatus == FormStatus.AddNew)
                    mstrRecID = "";
                else
                    mstrRecID = CvtFunc.ToString(this.dgvMain.CurrentRow.Cells["RecID"].Value);

                if (mFormStatus == FormStatus.Query) mFormStatus = FormStatus.Update;
                SetButtonStatus();
                toolStrip2.Enabled = true;
            }
            else if (e.TabPage == tabeOverview)
            {
                if (mFormStatus == FormStatus.Update || mFormStatus == FormStatus.AddNew)
                {
                    if (!UpdateData())
                    {
                        e.Cancel = true;
                        return;
                    }                   
                }
                else
                {
                    mstrRecID = "";
                }
                mFormStatus = FormStatus.Query;
                SetButtonStatus();
                dgvMain.RefreshData();
                toolStrip2.Enabled = false;
            }
            
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if ( mFormStatus== FormStatus.Update || mFormStatus == FormStatus.AddNew)  UpdateData(); 
            mFormStatus = FormStatus.AddNew;
            mstrRecID = "";
            InitControl();            
            SetButtonStatus();
            this.tbcMain.SelectTab(tabDetail);
            SetSickID();
            Contraindication();
            if (FromType == TitleType.SuddenDeafness)
            {
                cmbHasContraindication.Enabled = true;
                chkUseKFJS.Enabled = true;
                chkUseRS.Enabled = true;
                chkCuredInOtherHosiptal.Enabled = true;
                dtpFirstDate.Enabled = true;
                dtpPainDate.Enabled = true;
                btnSel.Enabled = true;
                SetContraindicationReadOnly(true);
            }            
            else
            {
                cmbHasContraindication.Enabled =true ;
                chkUseKFJS.Visible = false ;
                chkUseRS.Visible =false ;
                chkCuredInOtherHosiptal.Enabled = true;
                dtpFirstDate.Enabled = true;
                dtpPainDate.Enabled = true;
                btnSel.Visible =false ;
                SetContraindicationReadOnly(true);
            }
            if (FromType == TitleType.MeniereDisease)
            {
                btnSel.Visible = true;
                btnSel.Enabled = true;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            frmIncludeStandardMtn frmInclude = new frmIncludeStandardMtn();
            frmInclude.MdiParent = SysVar.mfrmMdiPar;
            frmInclude.Text = "入组标准";
            frmInclude.Show();
        }

        private bool  SelectCase()
        {
            string strSQL = string.Empty;
            strSQL = "select * from mSickPerson where sickrecid='" + txtSickRecID.Text  + "'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow == null) return false ;
            if (CvtFunc.ToString(drRow["CurOperation"]) == "")
            {
                if ( CvtFunc.ToString ( drRow["HasContraindication"]) == "") //如果没有选择禁忌症则要求选择
                {
                    MessageBox.Show("请选择此患者的禁忌症。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;               
                }
                else if (CvtFunc.ToString(drRow["HasContraindication"]) == "无")
                {
                    if (MessageBox.Show("此患者确定没有禁忌症？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        return true;
                    }
                }
                else
                {
                    strSQL = " select  * from mContraindication A inner join (select distinct ContraindicationName,Contraindication from dbo.mTherapeuticContraindication) B  " +
                             " on a.ContraindicationType=b.Contraindication " +
                             " where SickRecID='" + txtSickRecID + "' ";
                    DataTable dtDtl = mdbcon.GetDataTable(strSQL);
                    string strContraindication = string.Empty;
                    foreach (DataRow drDtl in dtDtl.Rows)
                    {
                        strContraindication += " \r\n" + CvtFunc.ToString(drDtl["ContraindicationName"]);

                    }
                    if (MessageBox.Show("此患者的禁忌症是:  " + strContraindication, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        return false;
                    }
                }
            }
            return true;
            
        }

        private void 治疗方案选择ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!UpdateData()) return;
            Form frmTherapeuticMethod = null;
            Type tpType=null ;
            string strSickRecId = txtSickRecID.Text;
            if (FromType == TitleType.SuddenDeafness)
            {
                if (!SelectCase()) return;
                tpType = Type.GetType("YT.XH.SuddenDeafness.frmTherapeuticMethodMtn,YT.XH.SuddenDeafness");
            }
            else if (FromType == TitleType.MeniereDisease)
            {
                tpType = Type.GetType("YT.XH.MeniereDisease.frmMDTherapeuticMethodMtn,YT.XH.MeniereDisease");
            }
            else if (FromType == TitleType.Dizziness)
            {
                tpType = Type.GetType("YT.XH.Dizziness.frmDZTherapeuticMethodMtn,YT.XH.Dizziness");
            }
            else if (FromType == TitleType.NOSE)
            {
                tpType = Type.GetType("YT.XH.Dizziness.frmNSTherapeuticMethodMtn,YT.XH.NOSE");
            }
            object[] args = new object[1];
            args[0] = strSickRecId;
            if (tpType == null) return;
            object mRouteObj = Activator.CreateInstance(tpType, args);
            if (tpType == null)
            {
                MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return ;
            }
            frmTherapeuticMethod = (Form)mRouteObj;
            //frmTherapeuticMethod.MdiParent = SysVar.mfrmMdiPar;
            frmTherapeuticMethod.ShowDialog();
            this.txtCurOperation.Text = DataFunctions.GetFieldValue("mSickPerson", "CurOperation", "SickRecID='" + txtSickRecID.Text + "'");
        }

        public bool UpdateCaseName()
        {
            this.txtCurOperation.Text = DataFunctions.GetFieldValue("mSickPerson", "CurOperation", "SickRecID='" + txtSickRecID.Text + "'");
            if (txtCurOperation.Text.Trim() != "")
            {
                cmbHasContraindication.Enabled = false;
                chkUseKFJS.Enabled = false;
                chkUseRS.Enabled = false;
                chkCuredInOtherHosiptal.Enabled = false;
                dtpFirstDate.Enabled = false;
                dtpPainDate.Enabled = false;
                btnSel.Enabled = false;
                SetContraindicationReadOnly(false);
            }
            else
            {
                cmbHasContraindication.Enabled = true;
                chkUseKFJS.Enabled = true;
                chkUseRS.Enabled = true;
                chkCuredInOtherHosiptal.Enabled = true;
                dtpFirstDate.Enabled = true;
                dtpPainDate.Enabled = true;
                btnSel.Enabled = true;
                SetContraindicationReadOnly(true);
            }
            return true;
        }

        private void btnSickHistory_Click(object sender, EventArgs e)
        {
            if ( FromType == TitleType.MeniereDisease )
            {
                Form frmSickHistory = null;
                Type tpType;
                string strSickRecId = txtSickRecID.Text;

                tpType = Type.GetType("YT.XH.MeniereDisease.frmSickHistory,YT.XH.MeniereDisease");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                 
                frmSickHistory = (Form)mRouteObj;
                frmSickHistory.Text = btnSickHistory.Text; 
                frmSickHistory.MdiParent = SysVar.mfrmMdiPar;
                frmSickHistory.Show();          
            
            }
            else if ( FromType == TitleType.SuddenDeafness )
            {
                Form frmSickHistory = null;
                Type tpType;
                string strSickRecId = txtSickRecID.Text;

                tpType = Type.GetType("YT.XH.SuddenDeafness.frmSDSickHistory,YT.XH.SuddenDeafness");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                frmSickHistory = (Form)mRouteObj;
                frmSickHistory.Text = btnSickHistory.Text;
                frmSickHistory.MdiParent = SysVar.mfrmMdiPar;
                frmSickHistory.Show();           
            }
            else if (FromType == TitleType.Dizziness)
            { 
                Form frmSickHistory = null;
                Type tpType;
                string strSickRecId = txtSickRecID.Text;

                tpType = Type.GetType("YT.XH.Dizziness.frmDZSickHistory,YT.XH.Dizziness");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                frmSickHistory = (Form)mRouteObj;
                frmSickHistory.Text = btnSickHistory.Text;
                frmSickHistory.MdiParent = SysVar.mfrmMdiPar;
                frmSickHistory.Show();           
            }
            
            
        }

        private void 知情同意书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           string strPath = Application.StartupPath;
           string strFileName = string.Empty ;
           
            if ( FromType == TitleType.NOSE )
                strFileName = strPath + "\\知情同意书_变应性鼻炎.doc";
            else if (FromType == TitleType.MeniereDisease )
                strFileName = strPath + "\\知情同意书_梅尼埃病.doc";
            else if (FromType == TitleType.SuddenDeafness )
                strFileName = strPath + "\\知情同意书.doc";
            else if ( FromType == TitleType.Dizziness  )
                strFileName = strPath + "\\知情同意书_良性阵发性位置性眩晕.doc";
            else
                return;
            System.Diagnostics.Process.Start(strFileName);   
        }

        private void btnExamBeforeTreat_Click(object sender, EventArgs e)
        {

            Form frmExamBeforeTreat = null;
            Type tpType;
            string strSickRecId = txtSickRecID.Text;

            if (FromType == TitleType.MeniereDisease)
            {
                tpType = Type.GetType("YT.XH.MeniereDisease.frmMDExamBeforeTreatMtn,YT.XH.MeniereDisease");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                frmExamBeforeTreat = (Form)mRouteObj;
                frmExamBeforeTreat.Text = btnSickHistory.Text;
                frmExamBeforeTreat.MdiParent = SysVar.mfrmMdiPar;
                frmExamBeforeTreat.Show();      
            }
            else if (FromType == TitleType.SuddenDeafness)
            {
                tpType = Type.GetType("YT.XH.SuddenDeafness.frmSDExamBeforeTreatMtn,YT.XH.SuddenDeafness");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                frmExamBeforeTreat = (Form)mRouteObj;
                frmExamBeforeTreat.Text = btnSickHistory.Text;
                frmExamBeforeTreat.MdiParent = SysVar.mfrmMdiPar;
                frmExamBeforeTreat.Show();      
            }
            else if (FromType == TitleType.Dizziness)
            {
                tpType = Type.GetType("YT.XH.Dizziness.frmDZExamBeforeTreatMtn,YT.XH.Dizziness");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                frmExamBeforeTreat = (Form)mRouteObj;
                frmExamBeforeTreat.Text = btnSickHistory.Text;
                frmExamBeforeTreat.MdiParent = SysVar.mfrmMdiPar;
                frmExamBeforeTreat.Show();      
            }
            else if (FromType == TitleType.NOSE)
            {

            }
            else
            {

            }
        }

        private void 眩晕障碍评估表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frmJacobsonMtn;
            Type tpType;
            string strSickRecId = txtSickRecID.Text;
            if (FromType == TitleType.SuddenDeafness)
            {
                tpType = Type.GetType("YT.XH.SuddenDeafness.frmSDJacobsonMtn,YT.XH.SuddenDeafness");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                frmJacobsonMtn = (Form)mRouteObj;
                frmJacobsonMtn.Text = btnSickHistory.Text;
                frmJacobsonMtn.MdiParent = SysVar.mfrmMdiPar;
                frmJacobsonMtn.Show();      
            }
            else if ( FromType == TitleType.MeniereDisease )
            {
                tpType = Type.GetType("YT.XH.MeniereDisease.frmMDJocobsonMtn,YT.XH.MeniereDisease");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                frmJacobsonMtn = (Form)mRouteObj;
                frmJacobsonMtn.Text = btnSickHistory.Text;
                frmJacobsonMtn.MdiParent = SysVar.mfrmMdiPar;
                frmJacobsonMtn.Show();  
            }
            else if (FromType == TitleType.Dizziness)
            { 
            
            }
        }

        private void btnUntowardEffect_Click(object sender, EventArgs e)
        {
            Form frmUntowardEffect;
            Type tpType;
            string strSickRecId = txtSickRecID.Text;
            if (FromType == TitleType.SuddenDeafness)
            {
                tpType = Type.GetType("YT.XH.SuddenDeafness.frmSDUntowardEffectTitle,YT.XH.SuddenDeafness");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                frmUntowardEffect = (Form)mRouteObj;                
                frmUntowardEffect.MdiParent = SysVar.mfrmMdiPar;
                frmUntowardEffect.Show();
            }
        }

        private void btnAfterTreate_Click(object sender, EventArgs e)
        {
            Form frmTreatmentEffectAfterTreate;
            Type tpType;
            string strSickRecId = txtSickRecID.Text;
            if (FromType == TitleType.SuddenDeafness)
            {
                tpType = Type.GetType("YT.XH.SuddenDeafness.frmSDTreatmentEffectAfterTreate,YT.XH.SuddenDeafness");
                object[] args = new object[1];
                args[0] = strSickRecId;

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                frmTreatmentEffectAfterTreate = (Form)mRouteObj;
                frmTreatmentEffectAfterTreate.MdiParent = SysVar.mfrmMdiPar;
                frmTreatmentEffectAfterTreate.Show();
            }
        }

        private void TreatmentEffect_SD(int intIndex)
        {
            string strTableName = "TreatmentEffectHdr";
            Form frmSDTreatmentEffectTitle;
            Type tpType=null ;
            string strSickRecId = txtSickRecID.Text;
            if (FromType == TitleType.SuddenDeafness)
            {
                if ( intIndex ==1 )
                    tpType = Type.GetType("YT.XH.SuddenDeafness.frmSDTreatmentEffectTitle,YT.XH.SuddenDeafness");
                else if ( intIndex ==2)
                    tpType = Type.GetType("YT.XH.SuddenDeafness.frmSDTreatmentEffectTitle_14,YT.XH.SuddenDeafness");
                else if ( intIndex ==3)
                    tpType = Type.GetType("YT.XH.SuddenDeafness.frmSDTreatmentEffectTitle_28,YT.XH.SuddenDeafness");


                object[] args ;

                if (DataFunctions.GetFieldValue(strTableName, "sickrecid", " sickrecid='" + strSickRecId + "' and SeqNo='" + intIndex + "' and type='" + TitleType.SuddenDeafness + "'") == "")
                {
                    args = new object[2];      
                    args[0] = strSickRecId;
                    args[1] = FormStatus.AddNew;
                }
                else
                {
                    args = new object[3];      
                    args[0] = strSickRecId;
                    args[1] = FormStatus.Update;
                    args[2] = intIndex.ToString()  ;
                }

                object mRouteObj = Activator.CreateInstance(tpType, args);
                if (tpType == null)
                {
                    MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                frmSDTreatmentEffectTitle = (Form)mRouteObj;
                frmSDTreatmentEffectTitle.ShowDialog();     
            }
        }

        private void btnTreatmentEffectTitle_Click(object sender, EventArgs e)
        {
            Form frmTreatmentEffectMtn;
            Type tpType=null 
                ;
            string strSickRecId = txtSickRecID.Text;
            if (FromType == TitleType.MeniereDisease )
            {
                tpType = Type.GetType("YT.XH.MeniereDisease.frmMDTreatmentEffectMtn,YT.XH.MeniereDisease");
               
            }
            else if (FromType == TitleType.SuddenDeafness)
            {
                tpType = Type.GetType("YT.XH.SuddenDeafness.frmSDTreatmentEffectMtn,YT.XH.SuddenDeafness");
            }
            else if (FromType == TitleType.Dizziness)
            {
                tpType = Type.GetType("YT.XH.Dizziness.frmDZTreatmentEffectMtn,YT.XH.Dizziness");
            }
            else
            {
                return;
            }

            object[] args = new object[1];
            args[0] = strSickRecId;
           
            object mRouteObj = Activator.CreateInstance(tpType, args);
            if (tpType == null)
            {
                MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            frmTreatmentEffectMtn = (Form)mRouteObj;
            frmTreatmentEffectMtn.MdiParent = SysVar.mfrmMdiPar;
            frmTreatmentEffectMtn.Show();
        }

        private void 第七天ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreatmentEffect_SD(1);
        }

        private void 第十四天ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreatmentEffect_SD(2);
        }

        private void 第二十八天ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreatmentEffect_SD(3);
        }

        private void btnHistory_Click(object sender, EventArgs e)
        {
            frmmCaseHistory frmHistory = new frmmCaseHistory(txtSickRecID.Text, FromType);
            frmHistory.ShowDialog();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void txtHospitalName_TextChanged(object sender, EventArgs e)
        {

        }

        private void CalDiffDay()
        {
            TimeSpan ts1 = new TimeSpan(CvtFunc.ToDateTime(dtpPainDate.Value).Ticks);
            TimeSpan ts2 = new TimeSpan(CvtFunc.ToDateTime(dtpFirstDate.Value).Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            txtDiffDay.Text = ts.Days.ToString();
        }

        private void dtpPainDate_ValueChanged(object sender, EventArgs e)
        {
            CalDiffDay();
        }

        private void chkCuredInOtherHosiptal_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCuredInOtherHosiptal.Checked)
            {
                chkUseRS.Visible = true;
                chkUseKFJS.Visible = true;
            }
            else
            {
                chkUseRS.Visible = false;
                chkUseRS.Checked = false;
                chkUseKFJS.Visible = false;
                chkUseKFJS.Checked = false;
            }
        }

        private void btnSel_Click(object sender, EventArgs e)
        {
           
                if (frmMContraindication != null)
                {
                    tbcMain.SelectTab(tabContraindication);
                }
          
        }

        private void cmbHasContraindication_SelectedValueChanged(object sender, EventArgs e)
        {
            if (FromType == TitleType.IgE) return;
            if (mFormStatus  == FormStatus.Query || mblnIsInSelecting ) return;
            if (cmbHasContraindication.Text == "有")
            {
                if (frmMContraindication != null)
                {
                    tbcMain.SelectTab(tabContraindication);
                }
            }
            else
            {
                string strSQL = "delete from mContraindication where SickRecID='" + txtSickRecID.Text + "'";
                mdbcon.ExecuteSql(strSQL);
                strSQL = "delete from mContraindicationDetail where SickRecID='" + txtSickRecID.Text + "'";
                mdbcon.ExecuteSql(strSQL);
                {
                    object obj = null;
                    Type[] ptype = new Type[1];
                    ptype.SetValue(typeof(String), 0);
                    object oRpt = frmMContraindication;
                    System.Reflection.MethodInfo mInfo = oRpt.GetType().GetMethod("FillContraindication", ptype);
                    object[] Param = { txtSickRecID.Text };
                    if (mInfo != null)
                    {
                        obj = mInfo.Invoke(oRpt, Param);
                    }
                }
            }
        }

        private void frmSickPersonMtn_Leave(object sender, EventArgs e)
        {
            if (mblnIsInUpdate == true) return;
            if (tbcMain.SelectedTab == tabContraindication || tbcMain.SelectedTab == tabDetail)
            {
                if (mFormStatus == FormStatus.Update || mFormStatus == FormStatus.AddNew)
                {               
                    UpdateData();
                }
                
            }
        }

        private void dtpBirth_ValueChanged(object sender, EventArgs e)
        {
            TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan ts2 = new TimeSpan(CvtFunc.ToDateTime(dtpBirth.Value).Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            txtAge.Text = (ts.Days / 365).ToString ();           
        }

        private void cmbHasContraindication_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtAge_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtAge_Leave(object sender, EventArgs e)
        {
            if (mFormStatus == FormStatus.AddNew)
            {
                dtpBirth.Value = DateTime.Now.AddYears(-CvtFunc.ToInt(txtAge.Text));
            }
        }

        private void frmSickPersonMtn_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.btnExit.Visible == false) e.Cancel = true;
        }

        private void dgvMain_QueryData(object sender, EventArgs e)
        {
            FillSeqNo();
        }


        private void FillSeqNo()
        {
            foreach (DataGridViewRow dgvRow in this.dgvMain.Rows)
            {
                dgvRow.Cells["序号"].Value  = (dgvRow.Index+1).ToString();
            }
        }

        private void btnSendMsg_Click(object sender, EventArgs e)
        {
            if (!UpdateData()) return;
            Form frmSendMsg = null;
            Type tpType = null;
            string strSickRecId = txtSickRecID.Text;
            if (strSickRecId.TrimEnd()== "") return;
            if (FromType == TitleType.NOSE)
            {
                tpType = Type.GetType("YT.XH.NOSE.frmNSSendMsg,YT.XH.NOSE");
            }
            
            object[] args = new object[1];
            args[0] = strSickRecId;
            if (tpType == null) return;
            object mRouteObj = Activator.CreateInstance(tpType, args);
            if (tpType == null)
            {
                MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            frmSendMsg = (Form)mRouteObj;
            //frmTherapeuticMethod.MdiParent = SysVar.mfrmMdiPar;
            frmSendMsg.ShowDialog();
        }

        private void btnAllMsg_Click(object sender, EventArgs e)
        {
            if (!UpdateData()) return;
            Form frmSendMsg = null;
            Type tpType = null;
            string strSickRecId = txtSickRecID.Text;
            if (strSickRecId.TrimEnd() == "") return;
            if (FromType == TitleType.NOSE)
            {
                tpType = Type.GetType("YT.XH.NOSE.frmNSMsgAnalysis,YT.XH.NOSE");
            }

            object[] args = new object[1];
            args[0] = strSickRecId;
            if (tpType == null) return;
            object mRouteObj = Activator.CreateInstance(tpType, args);
            if (tpType == null)
            {
                MessageBox.Show(" have not been done!", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            frmSendMsg = (Form)mRouteObj;
            //frmTherapeuticMethod.MdiParent = SysVar.mfrmMdiPar;
            frmSendMsg.ShowDialog();
        }
    }
}
